The goal of this lab is to explore methods for annotating and positioning with ggplot2 plots. This lab also utilizes scale_* to a greater degree which is part of our next reading. In fact, students may find going through/reading chapter 11 Colour scales and legends useful.
Datasets
We’ll be using the blue_jays.rda, titanic.rda, Aus_athletes.rda, and tech_stocks.rda datasets.
Use the athletes_dat dataset — extracted from Aus_althetes.rda — to recreate the following graphic as precisely as possible. Create the graphic twice: once using patchwork and once using cowplot.
Code
# Get list of sports played by BOTH sexesboth_sports <- Aus_athletes |># dataset of columns sex and sport # only unique observationsdistinct(sex, sport) |># see if sport is played by one gender or bothcount(sport) |># only want sports played by BOTH sexesfilter(n ==2) |># get list of sportspull(sport)# Process dataathletes_dat <- Aus_athletes |># only keep sports played by BOTH sexesfilter(sport %in% both_sports) |># rename track (400m) and track (sprint) to be track# case_when will be very useful with shiny appsmutate(sport =case_when( sport =="track (400m)"~"track", sport =="track (sprint)"~"track",TRUE~ sport ) )
Hints:
Build each plot separately
Bar plot: lower limit 0, upper limit 95
Bar plot: shift bar labels by 5 units and top justify
Bar plot: label size is 5
Bar plot: #D55E00D0 & #0072B2D0 — no alpha
Scatterplot: #D55E00D0 & #0072B2D0 — no alpha
Scatterplot: filled circle with “white” outline; size is 3
Scatterplot: rcc is red blood cell count; wcc is white blood cell count
Boxplot: outline #D55E00 and #0072B2; shading #D55E0040 and #0072B240
Boxplot: should be made narrower; 0.5
Boxplot: Legend is in top-right corner of bottom plot
Boxplot: Space out labels c("female ", "male")
Boxplot: Legend shading matches hex values for top two plots
Using patchwork
Solution
Code
bar_labels <- athletes_dat %>%count(sex)p1 <-ggplot(athletes_dat, aes(sex, fill = sex)) +geom_bar() +theme_minimal() +geom_text(data = bar_labels,#x = sex gets defaulted down from ggplot layer#we just need to specify the y location for textaes(y = n, label = n),nudge_y =-5,vjust ="top", size =5 ) +scale_y_continuous(name ="number", limits =c(0, 95),#removes vertical space below 0expand =c(0,0)) +scale_fill_manual(values =c("#D55E00D0", '#0072B2D0'), guide ="none") +scale_x_discrete(NULL,labels =c("female", "male"))p2 <-ggplot(athletes_dat, aes(rcc, wcc, fill = sex)) +geom_point(shape =21, color ="white", size =3) +theme_minimal() +labs(y ="WBC count", x ="RBC count") +scale_fill_manual(values =c("#D55E00D0", "#0072B2D0"),guide ="none")p3 <-ggplot(athletes_dat, aes(sport, pcBfat, color = sex, fill = sex)) +geom_boxplot(width =0.5) +scale_color_manual(NULL,values =c("#D55E00", "#0072B2"), labels =c("female", "male")) +scale_fill_manual(NULL, values =c("#D55E0040", "#0072B240"),labels =c("female", "male")) +theme_minimal() +theme(#anchor point of legend is top right corner of plot legend.justification =c(1,1),#location in the plot is top right legend.position =c(1,1),legend.direction ="horizontal" ) +guides(color =guide_legend(override.aes =list(color =NA, fill =c("#D55E00D0", "#0072B2D0")) )) +labs(x =NULL, y ="% body fat")(p1 + p2) / p3